package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.exception.InsertIntoEquipmentUseRecordTableException;
import edu.lsnu.equipment_management_system.exception.NoNotExistException;
import edu.lsnu.equipment_management_system.exception.NoPowerException;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.po.entity.EquipmentUseRecordTablePo;
import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import edu.lsnu.equipment_management_system.service.TableOperationService;
import edu.lsnu.equipment_management_system.service.TransactionalCollectionService;
import edu.lsnu.equipment_management_system.service.UpdateEquipmentRepairTableEndDateService;
import edu.lsnu.equipment_management_system.service.UpdateEquipmentUseRecordTableEndDateService;
import edu.lsnu.equipment_management_system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author mzy
 * @Description
 * 维修中设备确认维修完成的动作
 *      / 使用记录加上结束时间的动作（并开始一条新的使用记录） -> 设备转让
 * @date 2018/11/25
 */
@RestController
public class UseRecordAndRepairEndController {
    @Autowired
    UpdateEquipmentRepairTableEndDateService updateEquipmentRepairTableEndDateService;

    @Autowired
    TransactionalCollectionService transactionalCollectionService;
    /**
     * @Description 传入设备的编号，通过设备编号和用户编号，以及结束时间为空来进行条件约束
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  [equipmentNo, endDate]
     */
    @GetMapping("/api/user/repairEquipmentAddEndTime/{equipmentNo}")
    public Object confirmRepairAddEndTime(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        boolean res =
                updateEquipmentRepairTableEndDateService.repairToUsing(equipmentNo, userNo);

        if (res) {
            return JsonData.buildSuccess("ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 设备使用人改变需要做的的操作
     * post：
     *      注意 需要提交上来的是：
     *          equipmentNo：需要转让的设备
     *          userNo：新用户的userNo
     *          placeNo：存放场所的No[存放场所下拉框]
     *          remark：备注
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/changeUser-post") // String equipmentNo, String userNo, String placeNo, String remark,
    // @Transactional(propagation= Propagation.REQUIRED) 不要在controller中管理事务
    public Object changeUser(EquipmentUseRecordTablePo equipmentUseRecordTablePo, HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.equipmentChangeUser(equipmentUseRecordTablePo, session);
        } catch (NoPowerException e) {
            return JsonData.buildError(e.getMessage());
        } catch (NoNotExistException e) {
            return JsonData.buildError(e.getMessage());
        } catch (InsertIntoEquipmentUseRecordTableException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess("ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }

    }
}
